<!-- 主页 -->
<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'

//获取用户详细信息
import { userInfoService } from '@/api/user';
import useUserInfoStore from '@/store/userInfo';
import { useTokenStore } from '@/store/token';
import { ElMessage, ElMessageBox } from 'element-plus';

const userInfoStore = useUserInfoStore();
const token = useTokenStore();

const getUserInfo = async () => {
    let result = await userInfoService();
    userInfoStore.setInfo(result.data);
}
getUserInfo()

import { useRouter } from 'vue-router';
const router = useRouter()
const handleCommand = (command) => {
    if (command == 'logout') {
        //退出
        ElMessageBox.confirm(
            '您确认要退出吗?',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {
                //退出登录
                //清空pinia中的数据
                token.removeToken()
                userInfoStore.removeInfo()
                router.push('/login')
                let result = await varietyDeleteService(current, id);
                ElMessage({
                    type: 'success',
                    message: '退出登录成功',
                })
                varietyList()
            })
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '放弃退出登录',
                })
            })
    } else {
        //路由
        router.push('/user/' + command)
    }
}


</script>

<template>
    <!-- 容器 -->
    <el-container class="home-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <!-- 菜单 -->
            <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router accordion unique-opened>
                <!-- 宠物种类管理 -->
                <el-menu-item index="/variety/list">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>宠物种类管理</span>
                </el-menu-item>
                <!-- 宠物管理 -->
                <el-sub-menu index="1">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>宠物管理</span>
                    </template>
                    <el-menu-item index="/pet/add">
                        <el-icon>
                            <Management />
                        </el-icon>
                        <span>添加宠物</span>
                    </el-menu-item>
                </el-sub-menu>
                <!-- 宠物喂养文章管理 -->
                <el-menu-item index="/science">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>宠物喂养文章管理</span>
                </el-menu-item>
                <!-- 个人中心 -->
                <el-sub-menu index="2">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/user/userInfo">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/user/update">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>更换头像</span>
                    </el-menu-item>
                    <el-menu-item index="/user/updatePwd">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>用户：<strong>{{ userInfoStore.info.nickName }}</strong></div>
                <!-- 下拉菜单 -->
                <el-dropdown placement="bottom-end" @Command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.info.pic ? userInfoStore.info.pic : avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="userInfo" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="update" :icon="Crop">更换头像</el-dropdown-item>
                            <el-dropdown-item command="updatePwd" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
                    内容展示区
                </div> -->
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>宠物知识管理系统</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.home-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 120px;
            background: url('@/assets/logo.png') no-repeat center / 120px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>
